/**
 * 
 */
package com.hanhai.zrb.api.biz.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hanhai.zrb.api.biz.dao.mybatis.MessageInfoDao;
import com.hanhai.zrb.api.biz.dao.mybatis.MessageReceiveDao;
import com.hanhai.zrb.api.biz.service.MessageQueryService;
import com.hanhai.zrb.model.message.MessageInfo;
import com.hanhai.zrb.model.message.MessageReceive;

/**
 * @author ricky
 *
 */

@Service("messageQueryService")
public class MessageQueryServiceImpl implements MessageQueryService {


	@Resource
	private MessageInfoDao messageInfoDao;
	
	@Resource
	private MessageReceiveDao messageReceiverDao;
	
	@Override
	public List<MessageReceive> queryReceiveMessage(Long uid,Integer read) {
		
		List<MessageReceive> receiveList = getMessageListByReceiveUid(uid,read);
		buildInfo(receiveList);
		return receiveList;
	}
	
	@Override
	public List<MessageReceive> queryReceiveMessageByMaxId(Long receiverUid,Integer read,Long maxId,Integer limit){
		List<MessageReceive> receiveList = this.messageReceiverDao.queryByReceiveUidForMaxId(receiverUid, read, maxId, limit);
		buildInfo(receiveList);
		return receiveList;
	}
	
	@Override
	public PageInfo queryPageReceiveMessage(Long uid,Integer read,int pageSize,int pageNum) {
		PageHelper.startPage(pageNum, pageSize, true);
		
		List<MessageReceive> receiveList = getMessageListByReceiveUid(uid,read);
		buildInfo(receiveList);
		PageInfo pageInfo = new PageInfo(receiveList);
		return pageInfo;
	}
	
	

	@Override
	public List<MessageInfo> querySendMessage(Long uid,Integer status) {
		List<MessageInfo> sendList = getMessageListBySendUid(uid,status);
		return sendList;
	}
	@Override
	public int queryNewReceiveCount(Long uid){
		Integer count = queryReceiveCount(uid,0);
		return count;
	}
	
	private Integer queryReceiveCount(Long uid,Integer read){
		return 10;
	}
	
	private List<MessageReceive> getMessageListByReceiveUid(Long uid,Integer read){
		//List<MessageReceive> list = new ArrayList<MessageReceive>();
		List<MessageReceive> list = messageReceiverDao.queryByReceiveUid(uid, read);
		return list;
	}
	
	
	
	private List<MessageInfo> getMessageListBySendUid(Long uid,Integer read){
		List<MessageInfo> list = new ArrayList<MessageInfo>();
		//this.messageInfoDao.queryByIdList(idList)
		return list;
	}
	
	private Map<Long,MessageInfo> findByMessageInfoIdList(List<Long> ids){
		
		Map<Long,MessageInfo> map = new HashMap<Long,MessageInfo>();
		if( CollectionUtils.isEmpty(ids)){
			return map;
		}
		List<MessageInfo> list = messageInfoDao.queryByIdList(ids);
		for(MessageInfo info:list){
			map.put(info.getId(), info);
		}
		return map;
	}
	
	private void buildInfo(List<MessageReceive> list){
		
		List<Long> messageIdList = new ArrayList<Long>();
		for(MessageReceive receive:list){
			messageIdList.add(receive.getMessageInfoId());
		}
		
		Map<Long,MessageInfo> map = findByMessageInfoIdList(messageIdList);
		
		for(MessageReceive receive:list){
			MessageInfo tempMessageInfo = map.get(receive.getMessageInfoId());
			if(tempMessageInfo!=null){
				receive.setMessageInfo(tempMessageInfo);
			}
		}
	}

	@Override
	public boolean readMessage(Long receiverUid, Long readMessageReceiveId) {
		return 1==this.messageReceiverDao.updateRead(receiverUid, readMessageReceiveId);
	}

	@Override
	public int readMessageBatch(Long receiverUid,
			List<Long> readMessageReceiveIds) {
		return this.messageReceiverDao.updateReadBatch(receiverUid, readMessageReceiveIds);
	}

}
